package com.ksp.wke.kspr01;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

import static com.ksp.com.KSPCOM_BasConst.*;

import org.apache.lucene.queries.CommonTermsQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ksp.api.SpringUtil;

/**
 * 评论新增线程
 * @author anye
 *
 */
public class KSPR01Thd implements Runnable{
	
	private Logger log = LoggerFactory.getLogger(KSPR01Thd.class);

	ConcurrentLinkedQueue<Map<String, Object>> repQue = new ConcurrentLinkedQueue<>();
	
	public static ReentrantLock repDealLock = new ReentrantLock(true);	
	
	public KSPR01Thd() {
	}
	
	public KSPR01Thd(ConcurrentLinkedQueue<Map<String, Object>> pCmtQue) {
		this.repQue = pCmtQue;
	}
	
	@Override
	public void run() {
		while (!repQue.isEmpty()) {
			try {
				repDealLock.lock();
				Iterator<Map<String, Object>> cmtQueIte = repQue.iterator();
				while (cmtQueIte.hasNext()) {
					Map<String, Object> wCmtQueMap = cmtQueIte.next();
					exeCmtAddService(wCmtQueMap);
					repQue.poll();
				} 
			} catch (Exception e) {
				log.error(e.getMessage());
			} finally {
				repDealLock.unlock();
			}
		}
	}

	/**
	 * 多线程处理文章评论新增
	 * @param pCmtQueMap
	 */
	private void exeCmtAddService(Map<String, Object> pCmtQueMap) {
		KSPR01Mapper kspr01Mapper = SpringUtil.getBean(KSPR01Mapper.class);
		int insArtCmt = kspr01Mapper.insArtCmtRep(pCmtQueMap);
		if (insArtCmt > 0) {
			log.info(pCmtQueMap.get(FRO_OAM) + "评论"+ pCmtQueMap.get(CMT_TCH) +"成功");
		}
	}
}
